package co.example.michael.broadcastreceivertest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity implements View.OnClickListener {

    private Button customStaticButton;

    private Button customDynamicButton;

    private Button customSystemButton;

    private final static String DYNAMIC_RECEIVER = "dynamic";

    private final static String STATIC_RECEIVER = "static";

    private final static String SYSTEM_RECEIVER = "system";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        customDynamicButton = (Button) findViewById(R.id.custom_dynamic_button);
        customDynamicButton.setOnClickListener(this);

        customStaticButton = (Button) findViewById(R.id.custom_static_button);
        customStaticButton.setOnClickListener(this);

        customSystemButton = (Button) findViewById(R.id.custom_system_button);
        customSystemButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.custom_dynamic_button:
                Intent intent = new Intent();
                intent.setAction(DYNAMIC_RECEIVER);
                intent.putExtra("msg", "动态广播传送");
                sendBroadcast(intent);
                break;
            case R.id.custom_static_button:
                Intent intent1 = new Intent();
                intent1.setAction(STATIC_RECEIVER);
                intent1.putExtra("msg", "静态广播传送");
                sendBroadcast(intent1);
                break;
            case R.id.custom_system_button:
                Intent intent2 = new Intent();
                intent2.setAction(SYSTEM_RECEIVER);
                intent2.putExtra("msg", "系统广播传送");
                sendBroadcast(intent2);
                break;
            default:
                break;
        }
    }

    private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(DYNAMIC_RECEIVER)) {
                String msg = intent.getStringExtra("msg");
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
            }
        }
    };

    private BroadcastReceiver systemReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(SYSTEM_RECEIVER)) {
                String msg = intent.getStringExtra("msg");
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter dynamicFilter = new IntentFilter();
        dynamicFilter.addAction(DYNAMIC_RECEIVER);
        registerReceiver(dynamicReceiver,dynamicFilter);

        IntentFilter systemFilter = new IntentFilter();
        systemFilter.addAction(SYSTEM_RECEIVER);
        registerReceiver(systemReceiver,systemFilter);
    }
}

